package com.jzg.sys.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzg.sys.common.util.CommonUtils;
import com.jzg.sys.dao.entity.*;
import com.jzg.sys.dao.mapper.*;
import com.jzg.sys.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色:菜单 服务实现类
 * </p>
 *
 * @author lzp
 * @since 2019-02-22
 */
@Service
public class SecRoleMenuManager extends ServiceImpl<SecRoleMenuMapper, SecRoleMenu> {
    @Resource
    private SecRoleMapper roleMapper;
    @Resource
    private SecRoleMenuMapper roleMenuMapper;
    @Resource
    private SecRoleResourceMapper roleResourceMapper;
    @Resource
    private SecResourceMapper resourceMapper;
    @Resource
    private SecMenuMapper menuMapper;

    /**
     * @param secRole
     * @param page
     * @return
     * @Title: findPage
     * @Description: 角色分页查询
     * @Author: lzp 李志鵬
     * @Date: 2019/3/7
     */
    public IPage<SecRole> findPage(SecRole secRole, Page<SecRole> page) {
        return roleMapper.selectPage(page, new QueryWrapper<SecRole>().lambda()
                .eq(StringUtils.isNotBlank(secRole.getSysCode()), SecRole::getSysCode, secRole.getSysCode())
                .eq(StringUtils.isNotBlank(secRole.getRoleCode()), SecRole::getRoleCode, secRole.getRoleCode())
                .eq(StringUtils.isNotBlank(secRole.getRoleName()), SecRole::getRoleName, secRole.getRoleName())
                .orderByAsc(SecRole::getRoleCode)
        );
    }

    /**
     * @param roleId
     * @return
     * @Title: findCheckedKeys
     * @Description: 查询角色已拥有的菜单id
     * @Author: lzp 李志鵬
     * @Date: 2019/3/7
     */
    public List<String> findCheckedKeys(String roleId) {
        return roleMenuMapper.selectList(new LambdaQueryWrapper<SecRoleMenu>()
                .eq(SecRoleMenu::getRoleId, roleId))
                .stream()
                .map(SecRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }

    /**
     * @param secMenu
     * @return
     * @Title: saveRoleMenu
     * @Description: 保存角色菜单并授权
     * @Author: lzp 李志鵬
     * @Date: 2019/3/7
     */
    public void saveRoleMenu(SecMenu secMenu) {
        String[] menuIds = secMenu.getMenuIds().split(",");
        String roleId = secMenu.getRoleId();
        // 删除原有的角色资源
        roleResourceMapper.delete(new UpdateWrapper<SecRoleResource>().lambda()
                .eq(SecRoleResource::getRoleId, roleId)
        );
        // 删除角色菜单
        roleMenuMapper.delete(new UpdateWrapper<SecRoleMenu>().lambda()
                .eq(SecRoleMenu::getRoleId, roleId)
        );
        //新增角色菜单
        for (String menuId : menuIds) {
            SecRoleMenu roleMenu = new SecRoleMenu();
            roleMenu.setRoleMenuId(CommonUtils.getUUID());
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
        //新增角色资源
        for (String menuId : menuIds) {
            SecMenu menu = menuMapper.selectById(menuId);
            if(!menu.getIsLeaf())continue;//父节点不需要resource
            SecResource resource = resourceMapper.selectOne(new QueryWrapper<SecResource>().lambda()
                    .eq(SecResource::getMenuId, menuId)
                    .eq(SecResource::getResourceType, "01")
            );
            if (resource == null) throw new BusinessException(menuId + "菜单的资源不存在");
            SecRoleResource roleResource = new SecRoleResource();
            roleResource.setRoleResourceId(CommonUtils.getUUID());
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resource.getResourceId());
            roleResourceMapper.insert(roleResource);
        }
    }
}
